# 用户修改自己的班级昵称————updUserClassNickName
@app.route("/updUserClassNickName", methods=['PUT'])
def updUserClassNickName():
    data = request.get_data()
    json_data = json.loads(data.decode("UTF-8"))
    openid = json_data.get("openid")
    # print(openid)
    cid = json_data.get("cid")
    nickName = json_data.get("nickName")

    db = pymysql.connect(host="localhost",
                         user="root",
                         password="123456",
                         database="Ayh",
                         charset="utf8")
    # print("database yes")
    cursor = db.cursor()
    sql = "UPDATE classes_users SET nickName = '%s'  WHERE cid = '%d'" % (nickName, cid)  # 待更新

    try:
        cursor.execute(sql)
        db.commit()
        cursor.close()
        db.close()
        return jsonify({"status": 200})
    except Exception as e:
        print(e)
        db.rollback()
        cursor.close()
        db.close()
        return jsonify({"status": 500, "msg": 0})


# 用户退出已加入的班级————exitClass
@app.route("/exitClass", methods=['PUT'])
def exitClass():
    data = request.get_data()
    json_data = json.loads(data.decode("UTF-8"))
    openid = json_data.get("openid")
    cid = json_data.get("cid")
    db = pymysql.connect(host="localhost",
                         user="root",
                         password="123456",
                         database="Ayh",
                         charset="utf8")
    # print("database yes")
    cursor = db.cursor()
    sql = "DELETE FROM classes_users WHERE cid = '%d' " % cid
    sql1 = "UPDATE classes SET num = num - 1 WHERE(id= '%d' and uid = (SELECT id FROM users WHERE openid = '%s') )" % (cid, openid)
    try:
        cursor.execute(sql)
        cursor.execute(sql1)
        db.commit()
        cursor.close()
        db.close()
        return jsonify({"status": 200})
    except Exception as e:
        print(e)
        db.rollback()
        cursor.close()
        db.close()
        return jsonify({"status": 500, "msg": 0})



# 用户删除已创建的班级————delClass
@app.route("/delClass", methods=['PUT'])
def delClass():
    data = request.get_data()
    json_data = json.loads(data.decode("UTF-8"))
    openid = json_data.get("openid")
    cid = json_data.get("cid")
    db = pymysql.connect(host="localhost",
                         user="root",
                         password="123456",
                         database="Ayh",
                         charset="utf8")
    # print("database yes")
    cursor = db.cursor()
    sql = "DELETE FROM classes WHERE id = %d" % cid
    sql1 = "DELETE FROM classes_users WHERE cid = %d" % cid
    try:
        cursor.execute(sql)
        cursor.execute(sql1)
        db.commit()
        cursor.close()
        db.close()
        return jsonify({"status": 200})
    except Exception as e:
        print(e)
        db.rollback()
        cursor.close()
        db.close()
        return jsonify({"status": 500, "msg": 0})


# 用户删除班级成员————teaDelClassUser
@app.route("/teaDelClassUser", methods=['PUT'])
def teaDelClassUser():
    data = request.get_data()
    json_data = json.loads(data.decode("UTF-8"))
    openid = json_data.get("openid")
    cid = json_data.get("cid")
    uid = json_data.get("uid")
    db = pymysql.connect(host="localhost",
                         user="root",
                         password="123456",
                         database="Ayh",
                         charset="utf8")
    # print("database yes")
    cursor = db.cursor()
    sql = "DELETE FROM classes_users WHERE cid = %d and uid = %d" % (cid,uid)
    sql1 = "UPDATE classes SET num=num-1 WHERE uid = %d" % uid
    try:
        cursor.execute(sql)
        cursor.execute(sql1)
        db.commit()
        cursor.close()
        db.close()
        return jsonify({"status": 200})
    except Exception as e:
        print(e)
        db.rollback()
        cursor.close()
        db.close()
        return jsonify({"status": 500, "msg": 0})